import { Injectable } from '@angular/core';
import { Router, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router';

declare var ui: any;
declare var security: any;

/**
 * 定义整体框架的对外交互服务
 */
@Injectable()
export class InteractiveService {
    private initUrl = ['data_directory_mis'];
    // 当前的路由地址
    cr: string;
    // 全局是否加载中
    loadings = false;
    // 菜单控制器
    menu = null;
    // 地图状态: gis, mis, index
    tools = null;
    state = '';
    // 安全对象
    ss: any;

    constructor(public r: Router) {
        // 设置mis菜单选择样式
        this.menu = new ui.TabService('m', 'select');
        this.tools = new ui.TabService('layer', 'select');
        this.ss = security.SecurityService;
        this.optionUrlByRole(this.initUrl);
    }

    // 注册路由
    regRouter() {
        // 路由监听
        this.r.events.subscribe((event) => {
            if (event instanceof NavigationStart) {
                const sr = event.url.replace('/', '');
                if (sr === '') {
                    this.navigateByRole(this.initUrl);
                }
                this.loadings = true;
            }

            if (event instanceof NavigationEnd) {
                this.optionUrl(event.url);
                this.loadings = false;
            }
            if (event instanceof NavigationCancel) {
                this.loadings = false;
            }
            if (event instanceof NavigationError) {
                this.loadings = false;
            }
        });
    }

    // 地址导航
    navigate(url: string) {
        if (this.r.url === url) {
            return;
        }
        this.r.navigateByUrl(url);
    }

    outletNavigate(url: string) {
        if (this.r.url === url) {
            return;
        }
        this.r.navigate([{ outlets: { tools: [url] } }]);
    }

    // 地址导航，根据权限，选择第一个有权限的值
    navigateByRole(urls: string[]) {
        if (!this.ss.mi || this.ss.mi.sa) {
            this.navigate(urls[0]);
            return;
        }
        // tslint:disable-next-line:prefer-for-of
        for (let i = 0; i < urls.length; i++) {
            const url = urls[i];
            if (this.ss.mi.hasOwnProperty(url)) {
                this.navigate(url);
                return;
            }
        }
    }

    // 地址导航，根据权限，选择第一个有权限的值
    private optionUrlByRole(urls: string[]) {
        if (!this.ss.mi || this.ss.mi.sa) {
            this.optionUrl(urls[0]);
            return;
        }
        // tslint:disable-next-line:prefer-for-of
        for (let i = 0; i < urls.length; i++) {
            const url = urls[i];
            if (this.ss.mi.hasOwnProperty(url)) {
                this.optionUrl(url);
                return;
            }
        }
    }

    private optionUrl(url) {
        let sr = url.replace('/', '');
        const num = sr.indexOf('?');
        if (num > 0) {
            sr = sr.substr(0, num);
        }
        const rs: string[] = sr.split('_');
        if (rs.length === 3) {
            this.menu.setTab(rs[0]);
            this.state = rs[2];
        }
        this.cr = sr;
    }
}
